
import javax.swing.*;

import java.awt.FlowLayout;

import java.awt.event.*;

public class MyMouse2 extends JFrame {

    public JButton jb = new JButton("鼠标可拖动按钮");

    public JTextField jt = new JTextField();

    public MyMouse2() {

        jb.setBounds(100, 100, 330, 175);

        jt.setColumns(20);

        setBounds(100, 100, 350, 280);

        getContentPane().setLayout(new FlowLayout());

        getContentPane().add(jb);

        getContentPane().add(jt);

        addMouseMotionListener(new MouseMotionListener() {

            /**

             * 处理鼠标拖动事件

             * */

            public void mouseDragged(MouseEvent arg0) {

                mouseMoved(arg0);

                jb.setLocation(arg0.getPoint());

            }

            /**

             * 处理鼠标移动事件

             * */

            public void mouseMoved(MouseEvent arg0) {

                jt.setText(arg0.getPoint().toString());

            }

        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

        MyMouse2 test = new MyMouse2();

        test.setVisible(true);

    }

}

